package com.sinoglobal.ningxia.activity.vip;

import android.os.Bundle;
import android.view.View;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.amusement.AmusementDialog;
import com.sinoglobal.ningxia.activity.amusement.AmusementDialog.AmusementDialogClickListener;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.utils.ItktLog;
import com.sinoglobal.ningxia.utils.SharedPreferenceUtil;
import com.sinoglobal.ningxia.widget.ToggleSlipButton;
import com.sinoglobal.ningxia.widget.ToggleSlipButton.OnChangedListener;

/**
 * 
 * @title: PushMessageActivity.java
 * @package com.sinoglobal.ningxia.activity.vip
 * @description: (设置是否接收推送消息页面)
 *               isMessagePush表示是否推送系统消息，isActivityPush表示是否推送活动相关消息
 * @author lms
 * @date 2014-6-23 下午4:43:26
 */
public class MessagePush extends BaseActivity {
	// 推送消息、活动相关的开关按钮
	private ToggleSlipButton pushMessage, activity;
	// 评论相关是否开启
	private boolean isCommentCheck = false;
	// 系统消息推送是否开启
	private boolean isPushMessageCheck = false;
	// 活动相关推送是否开启
	private boolean isActivityCheck = false;
	// 关闭推送时的提示dialog
	private AmusementDialog alertDialog;
	private String message = "确定要关闭该消息的推送吗？";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_vip_push_message);
		openChenJInShi();//开启沉浸式
		init();
		getValFromNetwork();
		setListener();
	}

	private void init() {
		titleView.setText(R.string.push_message);
		pushMessage = (ToggleSlipButton) findViewById(R.id.push_slip_btn);
		activity = (ToggleSlipButton) findViewById(R.id.activity_slip_btn);
		isCommentCheck = SharedPreferenceUtil.getBoolean(FlyApplication.context, "isCommentCheck");
		isPushMessageCheck = SharedPreferenceUtil.getBoolean(FlyApplication.context, "isPushMessageCheck");
		isActivityCheck = SharedPreferenceUtil.getBoolean(FlyApplication.context, "isActivityCheck");
	}

	// 滑动按钮状态改变时设置监听（评论相关暂时不加）
	private void setListener() {
		// 滑动按钮的状态改变时记录状态，1为开，0为关
		// comment.SetOnChangedListener(new OnChangedListener() {
		//
		// @Override
		// public void OnChanged(boolean CheckState) {
		// if (CheckState) {
		// commentValue = 0;
		// LogUtil.i("==push===checkstate=======" + CheckState + 0);
		// SharedPreferenceUtil.saveBoolean(FlyApplication.context,
		// "isCommentCheck", true);
		// createDialog(0);
		// } else {
		// commentValue = 1;
		// LogUtil.i("==push===checkstate=======" + CheckState + 1);
		// SharedPreferenceUtil.saveBoolean(FlyApplication.context,
		// "isCommentCheck", false);
		// }
		// }
		// });

		// 系统消息推送
		pushMessage.SetOnChangedListener(new OnChangedListener() {

			@Override
			public void OnChanged(boolean CheckState) {
				if (CheckState) {
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isPushMessageCheck", true);
					createDialog(1);
				} else {
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isPushMessageCheck", false);
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isMessagePush", true);
				}
			}
		});

		// 活动相关消息推送
		activity.SetOnChangedListener(new OnChangedListener() {

			@Override
			public void OnChanged(boolean CheckState) {
				if (CheckState) {
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isActivityCheck", true);
					createDialog(2);
				} else {
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isActivityCheck", false);
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isActivityPush", true);
				}
			}
		});
	}

	// 设置滑动按钮状态
	private void setSlipButtonState() {
		ItktLog.i("---->" + isCommentCheck + "--" + isPushMessageCheck + "--" + isActivityCheck);
		// comment.setCheck(isCommentCheck);
		pushMessage.setCheck(isPushMessageCheck);
		activity.setCheck(isActivityCheck);
	}

	// 获取网络信息，得到推送开关按钮的状态
	private void getValFromNetwork() {
		boolean isMessagePush = SharedPreferenceUtil.getBoolean(FlyApplication.context, "isMessagePush");
		boolean isActivityPush = SharedPreferenceUtil.getBoolean(FlyApplication.context, "isActivityPush");
		if (!isMessagePush) {
			isPushMessageCheck = true;
			SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isPushMessageCheck", true);
			SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isMessagePush", false);
		} else {
			isPushMessageCheck = false;
			SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isPushMessageCheck", false);
			SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isMessagePush", true);
		}

		if (!isActivityPush) {
			isActivityCheck = true;
			SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isActivityCheck", true);
			SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isActivityPush", false);
		} else {
			isActivityCheck = false;
			SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isActivityCheck", false);
			SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isActivityPush", true);
		}
		// comment.setCheck(isCommentCheck);
		// pushMessage.setCheck(isPushMessageCheck);
		// activity.setCheck(isActivityCheck);
		setSlipButtonState();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	// 创建是否关闭消息推送的提示对话框
	public void createDialog(final int type) {
		alertDialog = new AmusementDialog(this, message, false);
		alertDialog.alertIvExpression.setVisibility(View.INVISIBLE);
		alertDialog.setText(getResources().getString(R.string.yes), getResources().getString(R.string.no));
		alertDialog.setAtdialogclick(new AmusementDialogClickListener() {
			@Override
			public void doConfirm() {
				alertDialog.cancel();
				switch (type) {
				case 0:
					// comment.setCheck(false);
					// comment.setOn2Off(true, false, false);// 点击取消滑动按钮变为开启状态
					// commentValue = 1;
					// SharedPreferenceUtil.saveBoolean(FlyApplication.context,
					// "isCommentCheck", false);
					break;
				case 1:
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isMessagePush", false);
					break;
				case 2:
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isActivityPush", false);
					break;
				}
			}

			@Override
			public void doCancel() {
				switch (type) {
				case 0:
					// comment.setCheck(false);
					// comment.setOn2Off(true, false, false);// 点击取消滑动按钮变为开启状态
					// commentValue = 1;
					// SharedPreferenceUtil.saveBoolean(FlyApplication.context,
					// "isCommentCheck", false);
					break;
				case 1:
					pushMessage.setCheck(false);
					// 点击取消滑动按钮变为开启状态
					pushMessage.setOn2Off(true, false, false);
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isPushMessageCheck", false);
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isMessagePush", true);
					break;
				case 2:
					activity.setCheck(false);
					// 点击取消滑动按钮变为开启状态
					activity.setOn2Off(true, false, false);
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isActivityCheck", false);
					SharedPreferenceUtil.saveBoolean(FlyApplication.context, "isActivityPush", true);
					break;
				}
				alertDialog.cancel();
			}
		});
		alertDialog.show();
	}
}
